<template>
	<view class="index"></view>
</template>

<script>
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	import {
		openidLogin,
		test_login,
		qy_login
	} from "@/api/user";
	export default {
		data() {
			return {
				code: ''
			}
		},
		onLoad() {
			this.onOpenidLogin()
		},
		methods: {
			qy_login() {
				let appid = "wwbe514664adcfafa1";
				let agentid = "1000041";
				let redirect_uri =
					"https://wechat.accortech.cn/static/meeting/index.html";
				let url =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${encodeURI(appid)}&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=code&scope=snsapi_privateinfo&state=STATE&agentid=${encodeURI(agentid)}#wechat_redirect`;
				window.location.href = url;
			},
			async onOpenidLogin() {
				const t = this
				uni.showLoading({
					title: '正在登录'
				})
				// #ifdef H5
				if (this.getQueryVariable("code") && !uni.getStorageSync('token')) {
					const code_data = await qy_login({
						code: this.getQueryVariable("code")
					})
					// const code_data = await test_login({user_id: 91})
					uni.hideLoading()
					if (code_data.code == 200) {
						uni.setStorageSync('token', code_data.token)
						uni.setStorageSync('userName', code_data.data.name || code_data.data.chinese_name)
						uni.setStorageSync('englishname', code_data.data.english_name)
						uni.setStorageSync('USERID', code_data.data.id)
						let pages = getCurrentPages()
						if (pages.length >= 2) {
							let current = pages[pages.length - 2]
							let {
								options,
								route
							} = current
							
							let url = `/${route}?redirect=1`
							let basePages = ['pages/indexBox/meetList']

							if (Object.keys(options).length > 0) {
								for (let key in options) {
									if (options.hasOwnProperty(key)) {
										url += `&${key}=${options[key]}`
									}
								}
							}
							
							if (basePages.indexOf(route) > -1) {
								uni.reLaunch({
									url: url
								})
							} else {
								uni.redirectTo({
									url: url
								})
							}
							return
						}
						uni.reLaunch({
							url: '/pages/indexBox/meetList'
						})
					}
				} else if(!this.getQueryVariable("code") && !uni.getStorageSync('token')) {
					this.qy_login();
				} else {
					uni.reLaunch({
						url: '/pages/indexBox/meetList'
					})
				}
				return
				// #endif
				uni.login({
					success: async code_res => {
						const code_data = await openidLogin({
							code: code_res.code
						})
						// const code_data = await test_login({user_id: 91})
						uni.hideLoading()
						if (code_data.code == 200) {
							uni.setStorageSync('token', code_data.token)
							uni.setStorageSync('userName', code_data.data.name)
							uni.setStorageSync('englishname', code_data.data.english_name)
							uni.setStorageSync('USERID', code_data.data.id)
							let pages = getCurrentPages()
							if (pages.length >= 2) {
								let current = pages[pages.length - 2]
								let {
									options,
									route
								} = current

								let url = `/${route}?redirect=1`
								let basePages = ['pages/indexBox/meetList']

								if (Object.keys(options).length > 0) {
									for (let key in options) {
										if (options.hasOwnProperty(key)) {
											url += `&${key}=${options[key]}`
										}
									}
								}
								if (basePages.indexOf(route) > -1) {
									uni.reLaunch({
										url: url
									})
								} else {
									uni.redirectTo({
										url: url
									})
								}
								return
							}
							uni.reLaunch({
								url: '/pages/indexBox/meetList'
							})
						} else {
							uni.showToast({
								icon: 'none',
								title: '登录失败!',
								duration: 0
							})
						}
					}
				})
			},
			getQueryVariable(variable) {
				var query = window.location.search.substring(1);
				var vars = query.split("&");
				for (var i = 0; i < vars.length; i++) {
					var pair = vars[i].split("=");
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return false;
			},
			goBack() {
				uni.navigateBack()
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					uni.setStorageSync('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					uni.setStorageSync('locale', 'zh')
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #070144;
	}

	.index {
		position: relative;
		padding-bottom: 110upx;
		box-sizing: border-box;

		.logo {
			width: 78upx;
			height: 64upx;
			padding-top: 99upx;
			margin-left: 30px;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.BG {
			position: absolute;
			top: 0;
			right: 0;
			width: 750upx;
			height: 1086upx;
			z-index: -1;
		}

		.info-text {
			width: 690upx;
			height: 667upx;
			padding-top: 99upx;
			margin: 0 auto;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.login {
			width: 362upx;
			height: 90upx;
			line-height: 90upx;
			text-align: center;
			margin: 94upx auto;
			color: #fff;
			font-size: 30upx;
			background-color: #d3a86a;
			border-radius: 10upx;
		}
	}
</style>